package dao;

import domain.User;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import util.JDBCUtils;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

public class UserDao {
    private JdbcTemplate jdbcTemplate =  new JdbcTemplate(JDBCUtils.getDataSource());


    public User login(String username ,String password){
        String sql = "select * from user where username = ? and password = ?";
        User user = null;
        try {
            user = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),username,password);
        } catch (Exception e) {
        }
        return user;
    }


    public void addComment(String username,String comment,String src){
        String sql = "insert into comment(src,content,username) values(?,?,?)";
        jdbcTemplate.update(sql,
                src,comment,username
        );
    }

    public List getComment(String src){
        String sql="select * from comment where src=?";
        List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql,src);
        return rows;
    }

    public List getAllCollectionZZY(String username) {
        String sql = "select src from collection where username=?";
        List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql,username);
        System.out.println(rows);
        return  rows;
    }

    public void addLike(String src){
        //update student set score=score+1 where id = 1
        String sql0 ="SELECT EXISTS(select likeNumb from likednumb where src=?)";
        boolean exists = jdbcTemplate.queryForObject(sql0,Boolean.class,src);
        if(exists){String sql= "update likednumb set likeNumb=likeNumb+1 where src = ?";
            jdbcTemplate.update(sql,src);}
        else {
            String sql = "insert into likednumb(src,likeNumb) values(?,?)";
            jdbcTemplate.update(sql,
                    src,1
            );
        }


    }

    public void addCollect(String src,String username){
        String sql = "insert into collection(src,username) values(?,?)";
        jdbcTemplate.update(sql,src,username);
    }

    public void reduceLike(String src){
        //update student set score=score+1 where id = 1
        String sql= "update likednumb set likeNumb=likeNumb-1 where src = ?";
        jdbcTemplate.update(sql,src);
    }

    public void reduceCollect(String src,String username){
        String sql="delete from collection  where src=? and username=?";
        jdbcTemplate.update(sql,src,username);
    }

    public void reduceLikeUser(String src,String username){
        String sql="delete from likeuser  where src=? and username=?";
        jdbcTemplate.update(sql,src,username);
    }

//    public String  getLike(String src){
//        System.out.println("这里是dao的getlike");
//        String sql= "select likeNumb from likednumb where src=?";
//        String likeNumb = jdbcTemplate.queryForObject(sql,String.class,src);
//        return likeNumb;
//    }


    public String  getLike(String src){
        String sql0 ="SELECT EXISTS(select likeNumb from likednumb where src=?)";
        boolean exists = jdbcTemplate.queryForObject(sql0,Boolean.class,src);
        if(exists){
        String sql= "select likeNumb from likednumb where src=?";
        String likeNumb = jdbcTemplate.queryForObject(sql,String.class,src);
        return likeNumb;}
        else return "0";
    }

    public String getComNumb(String src){
        String sql0="SELECT EXISTS(select * from comment where src=?)";
        boolean exists= jdbcTemplate.queryForObject(sql0,Boolean.class,src);
        if(exists){
            String sql="select * from comment where src=?";
            List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql,src);
            return String.valueOf(rows.size());
        }
        else return "0";
    }

    public String getCondition(String src,String username){
        String sql ="SELECT EXISTS(SELECT * FROM collection WHERE src=? and username=?)";
        boolean exists = jdbcTemplate.queryForObject(sql,Boolean.class,src,username);
        if(exists){
            return "true";
        }else {
            return "false";
        }
    }

    public String getLikeCondition(String src,String username){
        String sql = "SELECT EXISTS(SELECT * FROM likeuser WHERE src=? and username=?)";
        boolean exists = jdbcTemplate.queryForObject(sql,Boolean.class,src,username);
        if(exists){
            return "true";
        }else {
            return "false";
        }
    }


    public void  addLikeUser(String src,String username){
        String sql = "insert into likeuser(src,username) values(?,?)";
        jdbcTemplate.update(sql,src,username);
    }




    public User findByUsername(String username) {
        User user = null;
        try {
            //1.定义 sql
            String sql = "select * from user where username = ?";
            //2.执行 sql
            user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class),
                    username);
        } catch (Exception e) {
        }
        return user;
    }

    public void save(User user)
    {
        String sql = "insert into user(username,password,userid,useremail,userhead) values(?,?,?,?,?)";
        //2.执行 sql
        jdbcTemplate.update(sql,user.getUsername(),
                user.getPassword(),
                user.getUserid(),
                user.getUseremail(),
                user.getUserhead());
    }

    public User findByUsernameAndPassword(String username, String password) {
        User user = null;

        try {
            //1.定义 sql
            String sql = "select * from user where username = ? and password = ?";
            //2.执行 sql
            user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class),
                    username,password);
        } catch (Exception e) {
        }
        return user;
    }


}
